package com.xiaoxiao.springbootstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxiao.springbootstudy.dto.HisData;
import com.xiaoxiao.springbootstudy.mapper.OrderMapper;
import com.xiaoxiao.springbootstudy.pojo.Customer;
import com.xiaoxiao.springbootstudy.pojo.vo.CountResult;
import com.xiaoxiao.springbootstudy.service.CustomerService;
import com.xiaoxiao.springbootstudy.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author XiaoXiao
* @description 针对表【t_customer】的数据库操作Service实现
* @createDate 2025-05-13 14:45:56
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
    implements CustomerService{

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Transactional
    @Override
    public Map<String, Object> getCustList(Integer pageNum, Integer pageSize) {
        List<HisData> hisDatas = orderMapper.queryHisDataMapper();

        for(HisData hisData : hisDatas){
            Customer customer = new Customer();
            customer.setId(hisData.getCustId());
            customer.setHistory(hisData.getHisTotal());
            customerMapper.updateById(customer);
        }

        Map<String, Object> map = new HashMap<>();
        Page<Customer> page = new Page<>(pageNum, pageSize);
        List list = this.list(page);

        map.put("total", page.getTotal());
        map.put("rows", list);

        return map;
    }

    public List<Customer> getCustIdNameList() {

        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","cust_name");

        List<Customer> list = this.list(queryWrapper);

        return list;
    }

    @Override
    public List<CountResult> countCustService() {
        return customerMapper.countCustomerAreaMapper();
    }
}




